﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using NTQOnlineExam.Services.ReportService.Portfolio;
using NTQOnlineExam.Services.SearchCriteria;
using NTQOnlineExamFrontend.Helpers;
using NTQOnlineExamFrontend.Models;

namespace NTQOnlineExamFrontend.Controllers
{
    [Authorize]
    public class PortfolioController : Controller
    {
        private readonly IPortfolioReportService _portfolioReportService;
        public PortfolioController(IPortfolioReportService portfolioReportService)
        {
            _portfolioReportService = portfolioReportService;
        }

        [HttpGet]
        public ActionResult UserPortfolio()
        {
            IList<BreadcrumItem> breadcrumItems = new List<BreadcrumItem>();
            breadcrumItems.Add(new BreadcrumItem("Examination Administration", "/Member/Examination", false));
            breadcrumItems.Add(new BreadcrumItem("Exams Purchased", string.Empty, true));
            ViewData["Breadcrumb"] = breadcrumItems;

            return View("Portfolio", new PortfolioSearchViewModel());


        }

        [HttpGet]
        public ActionResult Search(string keyword, int pageIndex)
        {
            int total;
            var sortItems = Utility.GetSortItems(Request);
            var searchCriteria = new SearchCriteria() { Keyword = keyword };
            foreach (var item in sortItems)
            {
                searchCriteria.AddSortOrder(item.SortExpression, item.SortOrder);
            }
            var portfolios = _portfolioReportService.GetCurrentUserActivePortfolio(searchCriteria, true, pageIndex, PortfolioViewModel.PageSize, out total);
            var model = new PortfolioViewModel() { PortfolioDtos = portfolios, SelectedPage = pageIndex, TotalRecords = total };
            return PartialView("_Portfolio", model);
        }

        
    }
}
